home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / gdevpm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-04  |  35.3 KB  |  1,263 lines

  1. /* Copyright (C) 1992, 1993, 1994, 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevpm.c */
  20. /*
  21.  * OS/2 Presentation manager driver
  22.  * By Russell Lang (based on gdevmswn.c and gdevwdib.c)
  23.  *
  24.  * If Ghostscript is a PM application, stdin/stdout are not
  25.  * provided and so no text window is available.
  26.  * If Ghostscript is a windowed text application, a message queue
  27.  * can't be created so a PM window for graphics can't be created.
  28.  * The solution used here is to have two programs - gsos2.exe is a 
  29.  * text application and the outboard PM driver is gspmdrv.exe.
  30.  * Another solution may be to make Ghostscript a PM application
  31.  * and use VIO calls to provide a text window.
  32.  *
  33.  * If PM GSview starts Ghostscript, PM GSview displays the
  34.  * bitmap instead of the PM driver (gspmdrv.exe).
  35.  *
  36.  * Since Ghostscript is not a PM application, this driver creates a
  37.  * BMP bitmap in a named shared memory block and a second process
  38.  * gspmdrv.exe reads this memory block and provides the PM window.  
  39.  * Communication to gspmdrv.exe is via the shared memory block
  40.  * and semaphores.
  41.  */
  42.  
  43. #define INCL_DOS
  44. #define INCL_DOSERRORS
  45. #define INCL_WINWINDOWMGR
  46. #define INCL_DEV
  47. #define INCL_GPIBITMAPS
  48.  
  49. #include <os2.h>
  50. #include "string_.h"
  51. #include <stdlib.h>
  52. #include "gx.h"
  53. #include "gserrors.h"
  54. #include "gsexit.h"            /* for gs_exit_status */
  55. #include "gxdevice.h"
  56.  
  57. #include "gp.h"
  58. #include "gpcheck.h"
  59. #include "gsparam.h"
  60. #include "gdevpccm.h"
  61.  
  62. #include "gxdevmem.h"
  63. #include "gdevpm.h"
  64. #ifdef __DLL__
  65. #include "gsdll.h"
  66. #endif
  67.  
  68. #define MIN_COMMIT 4096    /* memory is committed in these size chunks */
  69. #define ID_NAME "GSPMDRV_%u_%u"
  70.  
  71. /* Initial values for width and height */
  72. #define INITIAL_RESOLUTION 96
  73. #define INITIAL_WIDTH (INITIAL_RESOLUTION * 85 / 10 + 1)
  74. #define INITIAL_HEIGHT (INITIAL_RESOLUTION * 11 + 1)
  75.  
  76. /* A macro for casting the device argument */
  77. #define pmdev ((gx_device_pm *)dev)
  78.  
  79.  
  80. #define pm_gsview_sizeof 80
  81. typedef struct gx_device_pm_s gx_device_pm;
  82.  
  83. #define gx_device_pm_common\
  84.     int BitsPerPixel;\
  85.     int alpha_text;\
  86.     int alpha_graphics;\
  87.     int UpdateInterval;\
  88.     char GSVIEW[pm_gsview_sizeof];\
  89.     BOOL dll;\
  90.     int nColors;\
  91.     BOOL updating;\
  92.     HTIMER update_timer;\
  93.     HEV sync_event;\
  94.     HEV next_event;\
  95.     HMTX bmp_mutex;\
  96.     HQUEUE drv_queue;\
  97.     HQUEUE term_queue;\
  98.     ULONG session_id;\
  99.     PID process_id;\
  100.     PID gspid;\
  101.     unsigned char *bitmap;\
  102.     ULONG committed;\
  103.     PBITMAPINFO2 bmi
  104.  
  105. /* The device descriptor */
  106. struct gx_device_pm_s {
  107.     gx_device_common;
  108.     gx_device_pm_common;
  109.     gx_device_memory mdev;
  110. };
  111.  
  112. /* Device procedures */
  113.  
  114. /* See gxdevice.h for the definitions of the procedures. */
  115. private dev_proc_open_device(pm_open);
  116. private dev_proc_get_initial_matrix(pm_get_initial_matrix);
  117. private dev_proc_sync_output(pm_sync_output);
  118. private dev_proc_output_page(pm_output_page);
  119. private dev_proc_close_device(pm_close);
  120. private dev_proc_map_rgb_color(pm_map_rgb_color);
  121. private dev_proc_map_color_rgb(pm_map_color_rgb);
  122. private dev_proc_fill_rectangle(pm_fill_rectangle);
  123. private dev_proc_copy_mono(pm_copy_mono);
  124. private dev_proc_copy_color(pm_copy_color);
  125. private dev_proc_get_bits(pm_get_bits);
  126. private dev_proc_get_params(pm_get_params);
  127. private dev_proc_put_params(pm_put_params);
  128. private dev_proc_get_alpha_bits(pm_get_alpha_bits);
  129.  
  130. private gx_device_procs pm_procs = {
  131.     pm_open,
  132.     pm_get_initial_matrix,
  133.     pm_sync_output,
  134.     pm_output_page,
  135.     pm_close,
  136.     pm_map_rgb_color,
  137.     pm_map_color_rgb,
  138.     pm_fill_rectangle,
  139.     NULL,            /* tile rectangle */
  140.     pm_copy_mono,
  141.     pm_copy_color,
  142.     NULL,            /* draw line */
  143.     pm_get_bits,
  144.     pm_get_params,
  145.     pm_put_params,
  146.     NULL,            /* map_cmyk_color */
  147.     gx_default_get_xfont_procs,
  148.     NULL,            /* get_xfont_device */
  149.     NULL,            /* map_rgb_alpha_color */
  150.     gx_page_device_get_page_device,
  151.     pm_get_alpha_bits
  152. };
  153. #ifdef __DLL__
  154. gx_device_pm far_data gs_os2dll_device = {
  155.     std_device_std_body(gx_device_pm, &pm_procs, "os2dll",
  156.       INITIAL_WIDTH, INITIAL_HEIGHT,
  157.       INITIAL_RESOLUTION, INITIAL_RESOLUTION),
  158.      { 0 },        /* std_procs */
  159.     8,        /* BitsPerPixel */
  160.     1, 1,        /* alpha */
  161.     5000,        /* UpdateInterval */
  162.     "\0",        /* GSVIEW */
  163.     1        /* is DLL device */
  164. };
  165. #endif
  166. gx_device_pm far_data gs_os2pm_device = {
  167.     std_device_std_body(gx_device_pm, &pm_procs, "os2pm",
  168.       INITIAL_WIDTH, INITIAL_HEIGHT,
  169.       INITIAL_RESOLUTION, INITIAL_RESOLUTION),
  170.      { 0 },        /* std_procs */
  171.     8,        /* BitsPerPixel */
  172.     1, 1,        /* alpha */
  173.     5000,        /* UpdateInterval */
  174.     "\0",        /* GSVIEW */
  175.     0        /* is not DLL device */
  176. };
  177.  
  178. /* Compress a gx_color_value into an 8-bit PM color value, */
  179. /* using only the high order 5 bits. */
  180. #define pm_color_value(z)\
  181.   ((((z) >> (gx_color_value_bits - 5)) << 3) +\
  182.    ((z) >> (gx_color_value_bits - 3)))
  183.  
  184. /* prototypes for internal procedures */
  185. private void pm_makepalette(gx_device_pm *);
  186. private void pm_update(gx_device_pm *);
  187. private uint pm_set_bits_per_pixel(gx_device_pm *, int);
  188. private uint pm_palette_size(gx_device_pm *);
  189. private int pm_alloc_bitmap(gx_device_pm *, gx_device *);
  190. private int pm_run_gspmdrv(gx_device_pm *);
  191. private void pm_write_bmp(gx_device_pm *);
  192.  
  193. /* Open the pm driver */
  194. int
  195. pm_open(gx_device *dev)
  196. {
  197.     int ccode;
  198.     CHAR id[128];
  199.     CHAR name[128];
  200.     PTIB pptib;
  201.     PPIB pppib;
  202.  
  203.     if (!pmdev->dll && (_osmode == DOS_MODE)) {
  204.         fprintf(stderr,"os2pm driver can't be used under DOS\n");
  205.         return gs_error_limitcheck;
  206.     }
  207.  
  208.     if (DosGetInfoBlocks(&pptib, &pppib)) {
  209.         fprintf(stderr,"\npm_open: Couldn't get pid\n");
  210.         return gs_error_limitcheck;
  211.     }
  212. #ifdef __DLL__
  213.     if (pppib->pib_ultype == 3)         /* if caller is PM app */
  214.         pmdev->gspid = pppib->pib_ulpid; /* use caller pid */
  215.     else
  216. #endif
  217.         pmdev->gspid = pppib->pib_ulppid; /* use parent (CMD.EXE) pid */
  218.     sprintf(id, ID_NAME, pmdev->gspid, (ULONG)dev);
  219.  
  220.     /* Allocate, but don't commit, enough memory for the largest */
  221.     /* possible bitmap (13Mbytes = A3 x 150dpi x 24bits) */
  222. #ifdef __DLL__
  223.     if (pmdev->dll) {
  224.         /* We don't need to use shared memory for the DLL */
  225.         if (DosAllocMem((PPVOID)&pmdev->bitmap, 
  226.         13*1024*1024, PAG_READ | PAG_WRITE)) {
  227.         fprintf(stderr,"pm_open: failed allocating BMP memory\n");
  228.         return gs_error_limitcheck;
  229.         }
  230.     }
  231.     else 
  232. #endif
  233.     {
  234.         /* Shared memory is common to all processes so we don't want to allocate too much */
  235.         sprintf(name, SHARED_NAME, *pmdev->GSVIEW ? pmdev->GSVIEW : id);
  236.         if (DosAllocSharedMem((PPVOID)&pmdev->bitmap, name,
  237.         13*1024*1024, PAG_READ | PAG_WRITE)) {
  238.         fprintf(stderr,"pm_open: failed allocating shared BMP memory %s\n", name);
  239.         return gs_error_limitcheck;
  240.         }
  241.     }
  242.     
  243.     /* commit one page so there is enough storage for a */
  244.     /* bitmap header and palette */
  245.     if (DosSetMem(pmdev->bitmap, MIN_COMMIT, PAG_COMMIT | PAG_DEFAULT)) {
  246.         DosFreeMem(pmdev->bitmap);
  247.         fprintf(stderr,"pm_open: failed committing BMP memory\n");
  248.         return gs_error_limitcheck;
  249.     }
  250.     pmdev->committed = MIN_COMMIT;
  251.  
  252.     if (pmdev->dll) {
  253.         /* Create mutex - used for preventing another thread from accessing */
  254.         /* bitmap while we are changing the bitmap size. Initially unowned. */
  255.         sprintf(name, MUTEX_NAME, id);
  256.         if (DosCreateMutexSem(name, &(pmdev->bmp_mutex), 0, FALSE)) {
  257.         DosFreeMem(pmdev->bitmap);
  258.         DosCloseEventSem(pmdev->sync_event);
  259.         DosCloseQueue(pmdev->drv_queue);
  260.         fprintf(stderr,"pm_open: failed to create mutex semaphore %s\n", name);
  261.         return gs_error_limitcheck;
  262.         }
  263.     }
  264.     else {
  265.       if (*pmdev->GSVIEW) {
  266.         APIRET rc;
  267.         /* GSview has already created the necessary objects */
  268.         /* so we use Open instead of Create */
  269.         rc = 0;
  270.         if (!rc) {
  271.             sprintf(name, NEXT_NAME, pmdev->GSVIEW);
  272.         rc = DosOpenEventSem(name, &pmdev->next_event);
  273.         }
  274.         if (!rc) {
  275.         sprintf(name, MUTEX_NAME, pmdev->GSVIEW);
  276.         rc = DosOpenMutexSem(name, &pmdev->bmp_mutex);
  277.         }
  278.         if (!rc) {
  279.         PID owner_pid;
  280.         sprintf(name, QUEUE_NAME, pmdev->GSVIEW);
  281.         rc = DosOpenQueue(&owner_pid, &pmdev->drv_queue, name);
  282.         }
  283.         if (rc) {
  284.         DosFreeMem(pmdev->bitmap);
  285.         DosCloseEventSem(pmdev->next_event);
  286.         fprintf(stderr, "pm_open: failed to open %s, rc = %u\n", name, rc);
  287.         return gs_error_limitcheck;
  288.         }
  289.       }
  290.       else {    /* not GSVIEW */
  291.         /* Create update event semaphore */
  292.         sprintf(name, SYNC_NAME, id);
  293.         if (DosCreateEventSem(name, &(pmdev->sync_event), 0, FALSE)) {
  294.         DosFreeMem(pmdev->bitmap);
  295.         fprintf(stderr,"pm_open: failed to create event semaphore %s\n", name);
  296.         return gs_error_limitcheck;
  297.         }
  298.         /* Create mutex - used for preventing gspmdrv from accessing */
  299.         /* bitmap while we are changing the bitmap size. Initially unowned. */
  300.         sprintf(name, MUTEX_NAME, id);
  301.         if (DosCreateMutexSem(name, &(pmdev->bmp_mutex), 0, FALSE)) {
  302.         DosFreeMem(pmdev->bitmap);
  303.         DosCloseEventSem(pmdev->sync_event);
  304.         DosCloseQueue(pmdev->drv_queue);
  305.         fprintf(stderr,"pm_open: failed to create mutex semaphore %s\n", name);
  306.         return gs_error_limitcheck;
  307.         }
  308.       }
  309.     }
  310.  
  311.     if ( (pm_set_bits_per_pixel(pmdev, pmdev->BitsPerPixel) < 0) ||
  312.          (gdev_mem_device_for_bits(dev->color_info.depth) == 0) )
  313.     {
  314.         if (!pmdev->dll) {
  315.         if (*pmdev->GSVIEW) {
  316.             DosCloseQueue(pmdev->drv_queue);
  317.             DosCloseEventSem(pmdev->next_event);
  318.         }
  319.         else
  320.             DosCloseEventSem(pmdev->sync_event);
  321.         }
  322.         DosCloseMutexSem(pmdev->bmp_mutex);
  323.         DosFreeMem(pmdev->bitmap);
  324.         return gs_error_limitcheck;
  325.     }
  326.  
  327.     /* initialise bitmap header */
  328.     pmdev->bmi = (PBITMAPINFO2)pmdev->bitmap;
  329.     pmdev->bmi->cbFix = 40;        /* OS/2 2.0 and Windows 3.0 compatible */
  330.     pmdev->bmi->cx = dev->width;
  331.     pmdev->bmi->cy = dev->height;
  332.     pmdev->bmi->cPlanes = 1;
  333.     pmdev->bmi->cBitCount = dev->color_info.depth;
  334.     pmdev->bmi->ulCompression = BCA_UNCOMP;
  335.     pmdev->bmi->cbImage = 0;
  336.     pmdev->bmi->cxResolution = (ULONG)(dev->x_pixels_per_inch / 25.4 * 1000);
  337.     pmdev->bmi->cyResolution = (ULONG)(dev->y_pixels_per_inch / 25.4 * 1000);
  338.     if (pmdev->BitsPerPixel <= 8) {
  339.         pmdev->bmi->cclrUsed = 1<<(pmdev->BitsPerPixel);
  340.         pmdev->bmi->cclrImportant = pmdev->nColors;
  341.     }
  342.     else {
  343.         pmdev->bmi->cclrUsed = 0;
  344.         pmdev->bmi->cclrImportant = 0;
  345.     }
  346.  
  347.     pm_makepalette(pmdev);
  348.  
  349.     /* commit pages */
  350.     ccode = pm_alloc_bitmap((gx_device_pm *)dev, dev);
  351.     if (ccode < 0) {
  352.         if (!pmdev->dll) {
  353.         if (*pmdev->GSVIEW) {
  354.             DosCloseQueue(pmdev->drv_queue);
  355.             DosCloseEventSem(pmdev->next_event);
  356.         }
  357.         else
  358.             DosCloseEventSem(pmdev->sync_event);
  359.         }
  360.         DosCloseMutexSem(pmdev->bmp_mutex);
  361.         DosFreeMem(pmdev->bitmap);
  362.         return ccode;
  363.     }
  364.  
  365.     if (*pmdev->GSVIEW)
  366.         return 0;    /* GSview will handle displaying */
  367.  
  368. #ifdef __DLL__
  369.     if (pmdev->dll) {
  370.         /* notify caller about new device */
  371.         (*pgsdll_callback)(GSDLL_DEVICE, (unsigned char *)pmdev, 1);
  372.         return 0;    /* caller will handle displaying */
  373.     }
  374. #endif
  375.  
  376.     ccode = pm_run_gspmdrv(pmdev);
  377.     if (ccode < 0) {
  378.         DosFreeMem(pmdev->bitmap);
  379.         DosCloseEventSem(pmdev->sync_event);
  380.         DosCloseMutexSem(pmdev->bmp_mutex);
  381.     }
  382.         
  383.     return ccode;
  384. }
  385.  
  386. /* Get the initial matrix.  BMPs, unlike most displays, */
  387. /* put (0,0) in the lower left corner. */
  388. private void
  389. pm_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
  390. {    pmat->xx = dev->x_pixels_per_inch / 72.0;
  391.     pmat->xy = 0;
  392.     pmat->yx = 0;
  393.     pmat->yy = dev->y_pixels_per_inch / 72.0;
  394.     pmat->tx = 0;
  395.     pmat->ty = 0;
  396.     if (*pmdev->GSVIEW)
  397.        pm_update((gx_device_pm *)dev);  /* let GSVIEW know we are drawing */
  398. }
  399.  
  400. /* Make the output appear on the screen. */
  401. int
  402. pm_sync_output(gx_device *dev)
  403. {
  404. #ifdef __DLL__
  405.     if (pmdev->dll) {
  406.     (*pgsdll_callback)(GSDLL_SYNC, (unsigned char *)dev, 0);
  407.     return 0;
  408.     }
  409. #endif
  410.  
  411.     /* tell gspmdrv or GSview process to update display */
  412.     if (*pmdev->GSVIEW) {
  413.     APIRET rc;
  414.     rc = DosWriteQueue(pmdev->drv_queue, GS_SYNC, 0, NULL, 0);
  415.     if (rc)
  416.         fprintf(stderr,"pm_sync_output: DosWriteQueue error %d\n",rc);
  417.     }
  418.     else {
  419.         if (pmdev->updating)
  420.             DosStopTimer(pmdev->update_timer);
  421.     DosPostEventSem(pmdev->sync_event);
  422.     }
  423.     pmdev->updating = FALSE;
  424.     return(0);
  425. }
  426.  
  427. /* Make the output appear on the screen  */
  428. /* and bring image window to foreground. */
  429. int
  430. pm_output_page(gx_device *dev, int copies, int flush)
  431. {
  432. int code;
  433. APIRET rc;
  434. #ifdef DEBUG
  435.     pm_write_bmp(pmdev);
  436. #endif
  437. #ifdef __DLL__
  438.         if (pmdev->dll) {
  439.         (*pgsdll_callback)(GSDLL_PAGE, (unsigned char *)dev, 0);
  440.         return 0;
  441.         }
  442. #endif
  443.  
  444.     if (*pmdev->GSVIEW) {
  445.         if (copies == -2) {
  446.                 rc = DosWriteQueue(pmdev->drv_queue, GS_END, 0, NULL, 0);
  447.                 if (rc)
  448.             fprintf(stderr,"pm_output_page: DosWriteQueue error %d\n",rc);
  449.         }
  450.         else if (copies == -1) {
  451.                 rc = DosWriteQueue(pmdev->drv_queue, GS_BEGIN, 0, NULL, 0);
  452.                 if (rc)
  453.             fprintf(stderr,"pm_output_page: DosWriteQueue error %d\n",rc);
  454.         }
  455.         else {
  456.             ULONG count;
  457.             pmdev->updating = FALSE;
  458.             /* signal GSview that another page is ready */
  459.                 rc = DosWriteQueue(pmdev->drv_queue, GS_PAGE, 0, NULL, 0);
  460.                 if (rc)
  461.             fprintf(stderr,"pm_output_page: DosWriteQueue error %d\n",rc);
  462.             /* wait for GSview to signal we can move on to next page */
  463.             DosWaitEventSem(pmdev->next_event, SEM_INDEFINITE_WAIT);
  464.             DosResetEventSem(pmdev->next_event, &count);
  465.         }
  466.         code = 0;
  467.     }
  468.     else {
  469.         code = pm_sync_output(dev);
  470.         rc = DosSelectSession(pmdev->session_id);
  471.         if (rc) {
  472.           DosSleep(2000);    /* give gspmdrv.exe a chance to run */
  473.           rc = DosSelectSession(pmdev->session_id);
  474.           if (rc == ERROR_SMG_NO_TARGET_WINDOW) {
  475.               DosSleep(5000);    /* give gspmdrv.exe a chance to run */
  476.               rc = DosSelectSession(pmdev->session_id);  /* try yet again */
  477.           }
  478.           if ( (rc == ERROR_SMG_SESSION_NOT_FOUND) ||
  479.          (rc == ERROR_SMG_INVALID_SESSION_ID) ){
  480.         /* someone has killed the session */
  481.         REQUESTDATA Request;
  482.         ULONG DataLength;
  483.         PVOID DataAddress;
  484.         PULONG QueueEntry;
  485.         BYTE ElemPriority;
  486.         /* Close gspmdrv driver */
  487.         DosStopSession(STOP_SESSION_SPECIFIED, pmdev->session_id);
  488.         Request.pid = pmdev->gspid;
  489.         Request.ulData = 0;
  490.         /* wait for termination queue, queue is then closed by session manager */
  491.         DosReadQueue(pmdev->term_queue, &Request, &DataLength, 
  492.             &DataAddress, 0, DCWW_WAIT, &ElemPriority, (HEV)NULL);
  493.         DosCloseQueue(pmdev->term_queue);
  494.         pmdev->term_queue = (HQUEUE)0;
  495.         /* restart it */
  496.         pm_run_gspmdrv(pmdev);
  497.         DosSleep(2000);    /* give gspmdrv.exe a chance to run */
  498.         rc = DosSelectSession(pmdev->session_id);
  499.           }
  500.           if (rc == ERROR_SMG_SESSION_NOT_FOREGRND)
  501.         DosBeep(400,50);
  502.           else if (rc)
  503.         fprintf(stderr,"pm_output_page: Select Session error code %u\n", rc);
  504.         }
  505.     }
  506.     return code;
  507. }
  508.  
  509. /* Close the pm driver */
  510. int
  511. pm_close(gx_device *dev)
  512. {
  513. APIRET rc;
  514. #ifdef __DLL__
  515.     if (pmdev->dll) {
  516.     DosRequestMutexSem(pmdev->bmp_mutex, 60000);
  517.         (*pgsdll_callback)(GSDLL_DEVICE, (unsigned char *)dev, 0);
  518.     DosReleaseMutexSem(pmdev->bmp_mutex);
  519.     }
  520.     else
  521. #endif
  522.     {
  523.     if (*pmdev->GSVIEW) {
  524.         if (gs_exit_status) {
  525.         ULONG count;
  526.         /* pause so error messages can be read */
  527.         DosResetEventSem(pmdev->next_event, &count);
  528.         DosWriteQueue(pmdev->drv_queue, GS_ERROR, 0, NULL, 0);
  529.         DosWaitEventSem(pmdev->next_event, SEM_INDEFINITE_WAIT);
  530.         DosResetEventSem(pmdev->next_event, &count);
  531.         }
  532.             rc = DosWriteQueue(pmdev->drv_queue, GS_CLOSE, 0, NULL, 0);
  533.             if (rc)
  534.         fprintf(stderr,"pm_close: DosWriteQueue error %d\n",rc);
  535.     }
  536.     else {
  537.         REQUESTDATA Request;
  538.         ULONG DataLength;
  539.         PVOID DataAddress;
  540.         PULONG QueueEntry;
  541.         BYTE ElemPriority;
  542.         /* Close gspmdrv driver */
  543.         DosStopSession(STOP_SESSION_SPECIFIED, pmdev->session_id);
  544.         Request.pid = pmdev->gspid;
  545.         Request.ulData = 0;
  546.         /* wait for termination queue, queue is then closed by session manager */
  547.         DosReadQueue(pmdev->term_queue, &Request, &DataLength, 
  548.         &DataAddress, 0, DCWW_WAIT, &ElemPriority, (HEV)NULL);
  549.         /* queue needs to be closed by us */
  550.         DosCloseQueue(pmdev->term_queue);
  551.     }
  552.     }
  553.     /* release memory */
  554.     DosFreeMem(pmdev->bitmap);
  555.     pmdev->bitmap = (unsigned char *)NULL;
  556.     pmdev->committed = 0;
  557.  
  558.     if (!pmdev->dll) {
  559.     /* close objects */
  560.     if (*pmdev->GSVIEW) {
  561.         DosCloseQueue(pmdev->drv_queue);
  562.         DosCloseEventSem(pmdev->next_event);
  563.     }
  564.     else {
  565.         DosCloseEventSem(pmdev->sync_event);
  566.         /* stop update timer */
  567.         if (pmdev->updating)
  568.         DosStopTimer(pmdev->update_timer);
  569.         pmdev->updating = FALSE;
  570.     }
  571.     }
  572.  
  573.     DosCloseMutexSem(pmdev->bmp_mutex);
  574.     return(0);
  575. }
  576.  
  577.  
  578. /* Map a r-g-b color to the colors available under PM */
  579. gx_color_index
  580. pm_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  581.   gx_color_value b)
  582. {
  583.     switch(dev->color_info.depth) {
  584.       case 24:
  585.         return    ((b >> (gx_color_value_bits - 8)) << 16) +
  586.             ((g >> (gx_color_value_bits - 8)) << 8) +
  587.             ((r >> (gx_color_value_bits - 8)));
  588.       case 8: {
  589.         int i;
  590.         RGB2 *prgb;
  591.         byte cr, cg, cb;
  592.  
  593.         /* map colors to 0->255 in 32 steps */
  594.         cr = pm_color_value(r);
  595.         cg = pm_color_value(g);
  596.         cb = pm_color_value(b);
  597.  
  598.         prgb = (RGB2 *)((PBYTE)pmdev->bmi + pmdev->bmi->cbFix);
  599.         /* search in palette */
  600.         for ( i = 0; i < pmdev->nColors; i++, prgb++ )
  601.         {    if ( !((cr ^ prgb->bRed) & 0xf8) &&
  602.                  !((cg ^ prgb->bGreen) & 0xf8) &&
  603.                  !((cb ^ prgb->bBlue) & 0xf8)
  604.                )
  605.                 return((gx_color_index)i);    /* found it */
  606.         }
  607.  
  608.         /* next try adding it to palette */
  609.         if (i < 230) { /* allow 26 for PM and other apps */
  610.             prgb->bRed = cr;
  611.             prgb->bGreen = cg;
  612.             prgb->bBlue = cb;
  613.             prgb->fcOptions = 0;
  614.             pmdev->nColors = i+1;
  615.             pmdev->bmi->cclrImportant = pmdev->nColors;
  616.                 if (*pmdev->GSVIEW) {
  617.                 APIRET rc;
  618.                 rc = DosWriteQueue(pmdev->drv_queue, GS_PALCHANGE, 0, NULL, 0);
  619.                 if (rc)
  620.                     fprintf(stderr,"pm_sync_output: DosWriteQueue error %d\n",rc);
  621.                 }
  622.             return((gx_color_index)i);    /* return new palette index */
  623.         }
  624.  
  625.         return(gx_no_color_index);  /* not found - dither instead */
  626.         }
  627.       case 4:
  628.         if ((r == g) && (g == b) && (r >= gx_max_color_value / 3 * 2 - 1)
  629.            && (r < gx_max_color_value / 4 * 3))
  630.             return ((gx_color_index)8);    /* light gray */
  631.         return pc_4bit_map_rgb_color(dev, r, g, b);
  632.     }
  633.     return (gx_default_map_rgb_color(dev,r,g,b));
  634. }
  635.  
  636. /* Map a color code to r-g-b. */
  637. int
  638. pm_map_color_rgb(gx_device *dev, gx_color_index color,
  639.   gx_color_value prgb[3])
  640. {    gx_color_value one;
  641.     switch(dev->color_info.depth) {
  642.       case 24:
  643.         one = (gx_color_value) (gx_max_color_value / 255);
  644.         prgb[0] = ((color)     & 255) * one;
  645.         prgb[1] = ((color>>8)  & 255) * one;
  646.         prgb[2] = ((color>>16) & 255) * one;
  647.         break;
  648.       case 8:
  649.         if (!dev->is_open)
  650.             return -1;
  651.         {
  652.         RGB2 *argb = (RGB2 *)((PBYTE)pmdev->bmi + pmdev->bmi->cbFix);
  653.         one = (gx_color_value) (gx_max_color_value / 255);
  654.         prgb[0] = argb[(int)color].bRed * one;
  655.         prgb[1] = argb[(int)color].bGreen * one;
  656.         prgb[2] = argb[(int)color].bBlue * one;
  657.         }
  658.         break;
  659.       case 4:
  660.         if (color == 8)    /* VGA light gray */
  661.             prgb[0] = prgb[1] = prgb[2] = (gx_max_color_value / 4 * 3);
  662.         else
  663.             pc_4bit_map_color_rgb(dev, color, prgb);
  664.         break;
  665.       default:
  666.         prgb[0] = prgb[1] = prgb[2] = 
  667.             (int)color ? gx_max_color_value : 0;
  668.     }
  669.     return 0;
  670. }
  671.  
  672. #define pmmdev ((gx_device *)&pmdev->mdev)
  673. #define pmmproc(proc) (*dev_proc(&pmdev->mdev, proc))
  674.  
  675. /* Fill a rectangle. */
  676. private int
  677. pm_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  678.   gx_color_index color)
  679. {
  680.     pmmproc(fill_rectangle)(pmmdev, x, y, w, h, color);
  681.     pm_update((gx_device_pm *)dev);
  682.     return 0;
  683. }
  684.  
  685. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  686. /* Color = gx_no_color_index means transparent (no effect on the image). */
  687. private int
  688. pm_copy_mono(gx_device *dev,
  689.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  690.   int x, int y, int w, int h,
  691.   gx_color_index zero, gx_color_index one)
  692. {
  693.     pmmproc(copy_mono)(pmmdev, base, sourcex, raster, id,
  694.                      x, y, w, h, zero, one);
  695.     pm_update((gx_device_pm *)dev);
  696.     return 0;
  697. }
  698.  
  699. /* Copy a color pixel map.  This is just like a bitmap, except that */
  700. /* each pixel takes 8 or 4 bits instead of 1 when device driver has color. */
  701. private int
  702. pm_copy_color(gx_device *dev,
  703.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  704.   int x, int y, int w, int h)
  705. {
  706.     pmmproc(copy_color)(pmmdev, base, sourcex, raster, id,
  707.                       x, y, w, h);
  708.     pm_update((gx_device_pm *)dev);
  709.     return 0;
  710. }
  711.  
  712. int
  713. pm_get_bits(gx_device *dev, int y, byte *str, byte **actual_data)
  714. {
  715.     return pmmproc(get_bits)(pmmdev, y, str, actual_data);
  716. }
  717. /* Get PM parameters */
  718. int
  719. pm_get_params(gx_device *dev, gs_param_list *plist)
  720. {    int code = gx_default_get_params(dev, plist);
  721.     gs_param_string gvs;
  722.     gvs.data = pmdev->GSVIEW, gvs.size = strlen(gvs.data),
  723.       gvs.persistent = false;
  724.     code < 0 ||
  725.     (code = param_write_int(plist, "UpdateInterval", &pmdev->UpdateInterval)) < 0 ||
  726.     (code = param_write_string(plist, "GSVIEW", &gvs)) < 0;
  727.     return code;
  728. }
  729.  
  730. /* Put parameters. */
  731. private int
  732. pm_put_alpha_param(gs_param_list *plist, gs_param_name param_name, int *pa,
  733.   bool alpha_ok)
  734. {    int code = param_read_int(plist, param_name, pa);
  735.     switch ( code )
  736.     {
  737.     case 0:
  738.         switch ( *pa )
  739.           {
  740.           case 1:
  741.             return 0;
  742.           case 2: case 4:
  743.             if ( alpha_ok )
  744.               return 0;
  745.           default:
  746.             code = gs_error_rangecheck;
  747.           }
  748.     default:
  749.         param_signal_error(plist, param_name, code);
  750.     case 1:
  751.         ;
  752.     }
  753.     return code;
  754. }
  755.  
  756.  
  757. /* Set PM parameters -- size and resolution. */
  758. /* We implement this ourselves so that we can do it without */
  759. /* closing and opening the device. */
  760. /* Also set BitsPerPixel and GSVIEW if device not open */
  761. int
  762. pm_put_params(gx_device *dev, gs_param_list *plist)
  763. {    int ecode = 0, code;
  764.     bool reopen = false;
  765.     bool is_open = dev->is_open;
  766.     int width = dev->width;
  767.     int height = dev->height;
  768.     int old_bpp = dev->color_info.depth;
  769.     int bpp = old_bpp;
  770.     int uii = pmdev->UpdateInterval;
  771.     gs_param_string gsvs;
  772.     int atext = pmdev->alpha_text, agraphics = pmdev->alpha_graphics;
  773.     bool alpha_ok;
  774.  
  775.     /* Handle extra parameters */
  776.     switch ( code = param_read_string(plist, "GSVIEW", &gsvs) )
  777.     {
  778.     case 0:
  779.         if ( gsvs.size == strlen(pmdev->GSVIEW) &&
  780.              !memcmp(pmdev->GSVIEW, gsvs.data, gsvs.size)
  781.            )
  782.           {    gsvs.data = 0;
  783.             break;
  784.           }
  785.         if ( dev->is_open )
  786.           ecode = gs_error_rangecheck;
  787.         else if ( gsvs.size >= pm_gsview_sizeof )
  788.           ecode = gs_error_limitcheck;
  789.         else
  790.           break;
  791.         goto gsve;
  792.     default:
  793.         ecode = code;
  794. gsve:        param_signal_error(plist, "GSVIEW", ecode);
  795.     case 1:
  796.         gsvs.data = 0;
  797.         break;
  798.     }
  799.  
  800.     switch ( code = param_read_int(plist, "UpdateInterval", &uii) )
  801.     {
  802.     case 0:
  803.         if ( uii < 0 )
  804.           ecode = gs_error_rangecheck;
  805.         else
  806.           break;
  807.         goto uie;
  808.     default:
  809.         ecode = code;
  810. uie:        param_signal_error(plist, "UpdateInterval", ecode);
  811.     case 1:
  812.         break;
  813.     }
  814.  
  815.     switch ( code = param_read_int(plist, "BitsPerPixel", &bpp) )
  816.     {
  817.     case 0:
  818.         if ( dev->is_open && bpp != old_bpp )
  819.           ecode = gs_error_rangecheck;
  820.         else
  821.           {    code = pm_set_bits_per_pixel(pmdev, bpp);
  822.             if ( code < 0 )
  823.               ecode = code;
  824.             else
  825.               break;
  826.           }
  827.         goto bppe;
  828.     default:
  829.         ecode = code;
  830. bppe:        param_signal_error(plist, "BitsPerPixel", ecode);
  831.     case 1:
  832.         break;
  833.     }
  834.  
  835.     alpha_ok = pmdev->color_info.depth >= 8;
  836.     if ( (code = pm_put_alpha_param(plist, "TextAlphaBits", &pmdev->alpha_text, alpha_ok)) < 0 )
  837.       ecode = code;
  838.     if ( (code = pm_put_alpha_param(plist, "GraphicsAlphaBits", &pmdev->alpha_graphics, alpha_ok)) < 0 )
  839.       ecode = code;
  840.  
  841.     if ( ecode >= 0 )
  842.       {    /* Prevent gx_default_put_params from closing the device. */
  843.         dev->is_open = false;
  844.         ecode = gx_default_put_params(dev, plist);
  845.         dev->is_open = is_open;
  846.       }
  847.     if ( ecode < 0 )
  848.       {
  849.         if ( bpp != old_bpp )
  850.           pm_set_bits_per_pixel(pmdev, old_bpp);
  851.           pmdev->alpha_text = atext;
  852.           pmdev->alpha_graphics = agraphics;
  853.         return ecode;
  854.       }
  855.  
  856.     /* Hand off the change to the implementation. */
  857.     /* obtain mutex - to prevent gspmdrv from using bitmap */
  858.     /* while we change its size */
  859.     if (DosRequestMutexSem(pmdev->bmp_mutex, 20000) == ERROR_TIMEOUT)
  860.         fprintf(stderr, "pm_put_params: mutex timeout\n");
  861.     if ( is_open && (old_bpp != bpp ||
  862.              dev->width != width || dev->height != height)
  863.        )
  864.     {    int ccode;
  865.         ccode = pm_alloc_bitmap(pmdev, dev);
  866.         if ( ccode < 0 )
  867.         {    /* Bad news!  Some of the other device parameters */
  868.             /* may have changed.  We don't handle this. */
  869.             /* This is ****** WRONG ******. */
  870.             dev->width = width;
  871.             dev->height = height;
  872.               pm_set_bits_per_pixel(pmdev, old_bpp);
  873.             pmdev->alpha_text = atext;
  874.             pmdev->alpha_graphics = agraphics;
  875.             pm_alloc_bitmap(pmdev, dev);
  876.             DosReleaseMutexSem(pmdev->bmp_mutex);
  877.             return ccode;
  878.         }
  879.         reopen = true;
  880.     }
  881.     pmdev->UpdateInterval = uii;
  882.     if ( gsvs.data != 0 )
  883.       {    memcpy(pmdev->GSVIEW, gsvs.data, gsvs.size);
  884.         pmdev->GSVIEW[gsvs.size] = 0;
  885.       }
  886.  
  887.     if ( dev->is_open && reopen ) {
  888.         /* need to update bitmap info header also */
  889.         pmdev->bmi->cx = dev->width;
  890.         pmdev->bmi->cy = dev->height;
  891.         /* update bit count and palette */
  892.         pmdev->bmi->cBitCount = dev->color_info.depth;
  893.         pmdev->bmi->cclrUsed = 1<<(pmdev->BitsPerPixel);
  894.         pmdev->bmi->cclrImportant = pmdev->nColors;
  895.         pm_makepalette(pmdev);
  896.         /* erase bitmap - before window gets redrawn */
  897.         (*dev_proc(dev, fill_rectangle))(dev,
  898.         0, 0, dev->width, dev->height,
  899.         pm_map_rgb_color(dev, gx_max_color_value, 
  900.             gx_max_color_value, gx_max_color_value));
  901.         /* cause scroll bars to be redrawn */
  902.         /* need to signal gspmdrv that bitmap size has changed */
  903.         /* or perhaps gspmdrv can check if the bitmap size has */
  904.         /* before each use */
  905.  
  906. #ifdef __DLL__
  907.         if (pmdev->dll)
  908.             (*pgsdll_callback)(GSDLL_SIZE, (unsigned char *)dev,
  909.             (dev->width & 0xffff) + ( (dev->height & 0xffff)<<16) );
  910. #endif
  911.     }
  912.  
  913.     /* release bmp mutex */
  914.     DosReleaseMutexSem(pmdev->bmp_mutex);
  915.     return 0;
  916. }
  917.  
  918.  
  919. /* Get the number of alpha bits. */
  920. int
  921. pm_get_alpha_bits(gx_device *dev, graphics_object_type type)
  922. {    return (type == go_text ? pmdev->alpha_text : pmdev->alpha_graphics);
  923. }
  924.  
  925.  
  926. #ifdef __DLL__
  927. /* ------ DLL routines ------ */
  928. /* store at pbitmap the address of the bitmap */
  929. /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */
  930. unsigned long GSDLLAPI
  931. gsdll_get_bitmap(unsigned char *device, unsigned char **pbitmap)
  932. {
  933. gx_device *dev = (gx_device *)device;
  934.     *pbitmap =  (unsigned char *)(pmdev->bmi );
  935.     return 0;
  936. }
  937.  
  938. /* Lock the device (so it's size cannot be changed) if flag = TRUE */
  939. /* or unlock the device if flag = FALSE */
  940. /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */
  941. int GSDLLAPI
  942. gsdll_lock_device(unsigned char *device, int flag)
  943. {
  944. gx_device *dev = (gx_device *)device;
  945. APIRET rc;
  946.     if (flag)
  947.     rc = DosRequestMutexSem(pmdev->bmp_mutex, 60000);
  948.     else
  949.     rc = DosReleaseMutexSem(pmdev->bmp_mutex);
  950.     return rc;
  951. }
  952.  
  953. #endif /* __DLL__ */
  954.  
  955. /* ------ Internal routines ------ */
  956.  
  957. #undef pmdev
  958.  
  959.  
  960. /* start gspmdrv.exe */
  961. private int
  962. pm_run_gspmdrv(gx_device_pm *pmdev)
  963. {
  964.     int ccode;
  965.     PCHAR pdrvname = "gspmdrv.exe";
  966.     CHAR error_message[256];
  967.     CHAR term_queue_name[128];
  968.     CHAR id[128];
  969.     CHAR arg[1024];
  970.     STARTDATA sdata;
  971.     APIRET rc;
  972.     PTIB pptib;
  973.     PPIB pppib;
  974.     CHAR progname[256];
  975.     PCHAR tail;
  976.  
  977.     sprintf(id, ID_NAME, pmdev->gspid, (ULONG)pmdev);
  978.  
  979.     /* Create termination queue - used to find out when gspmdrv terminates */
  980.     sprintf(term_queue_name, "\\QUEUES\\TERMQ_%s", id);
  981.     if (DosCreateQueue(&(pmdev->term_queue), QUE_FIFO, term_queue_name)) {
  982.         fprintf(stderr,"pm_run_gspmdrv: failed to create termination queue\n");
  983.         return gs_error_limitcheck;
  984.     }
  985.  
  986.     /* get full path to gsos2.exe and hence path to gspmdrv.exe */
  987.     if ( (rc = DosGetInfoBlocks(&pptib, &pppib)) != 0 ) {
  988.         fprintf(stderr,"pm_run_gspmdrv: Couldn't get module handle, rc = %d\n", rc);
  989.         return gs_error_limitcheck;
  990.     }
  991.     if ( (rc = DosQueryModuleName(pppib->pib_hmte, sizeof(progname)-1, progname)) != 0 ) {
  992.         fprintf(stderr,"pm_run_gspmdrv: Couldn't get module name, rc = %d\n", rc);
  993.         return gs_error_limitcheck;
  994.     }
  995.     if ((tail = strrchr(progname,'\\')) != (PCHAR)NULL)
  996.     {
  997.         tail++;
  998.         *tail = '\0';
  999.     }
  1000.     else 
  1001.         tail = progname;
  1002.     strcat(progname, pdrvname);
  1003.  
  1004.     /* Open the PM driver session gspmdrv.exe */
  1005.     /* arguments are: */
  1006.     /*  (1) -d (display) option */
  1007.     /*  (2) id string */
  1008.     sprintf(arg, "-d %s", id);
  1009.  
  1010.     /* because gspmdrv.exe is a different EXE type to gs.exe, 
  1011.      * we must use start session not DosExecPgm() */
  1012.     sdata.Length = sizeof(sdata);
  1013.     sdata.Related = SSF_RELATED_CHILD;    /* to be a child  */
  1014.     sdata.FgBg = SSF_FGBG_BACK;        /* start in background */
  1015.     sdata.TraceOpt = 0;
  1016.     sdata.PgmTitle = "Ghostscript PM driver session";
  1017.     sdata.PgmName = progname;
  1018.     sdata.PgmInputs = arg;
  1019.     sdata.TermQ = term_queue_name;
  1020.     sdata.Environment = pppib->pib_pchenv;    /* use Parent's environment */
  1021.     sdata.InheritOpt = 0;            /* Can't inherit from parent because different sesison type */
  1022.     sdata.SessionType = SSF_TYPE_DEFAULT;    /* default is PM */
  1023.     sdata.IconFile = NULL;
  1024.     sdata.PgmHandle = 0;
  1025.     sdata.PgmControl = 0;
  1026.     sdata.InitXPos = 0;
  1027.     sdata.InitYPos = 0;
  1028.     sdata.InitXSize = 0;
  1029.     sdata.InitYSize = 0;
  1030.     sdata.ObjectBuffer = error_message;
  1031.     sdata.ObjectBuffLen = sizeof(error_message);
  1032.  
  1033.     rc = DosStartSession(&sdata, &pmdev->session_id, &pmdev->process_id);
  1034.     if (rc == ERROR_FILE_NOT_FOUND) {
  1035.         sdata.PgmName = pdrvname;
  1036.         rc = DosStartSession(&sdata, &pmdev->session_id, &pmdev->process_id);
  1037.     }
  1038.     if (rc) {
  1039.         fprintf(stderr,"pm_run_gspmdrv: failed to run %s, rc = %d\n", sdata.PgmName, rc);
  1040.         fprintf(stderr,"pm_run_gspmdrv: error_message: %s\n", error_message);
  1041.         return gs_error_limitcheck;
  1042.     }
  1043.  
  1044.     return 0;
  1045.  
  1046. }
  1047.  
  1048. /* Allocate the backing bitmap. */
  1049. private int
  1050. pm_alloc_bitmap(gx_device_pm *pmdev, gx_device *param_dev)
  1051. {
  1052.     gx_device_memory mdev;
  1053.     byte *base;
  1054.     ulong data_size;
  1055.     uint ptr_size;
  1056.     uint pal_size;
  1057.     uint raster;
  1058.     ULONG rc;
  1059.     ULONG needed;
  1060.  
  1061.     /* Finish initializing the bitmap. */
  1062.  
  1063.     gs_make_mem_device(&mdev, gdev_mem_device_for_bits(pmdev->color_info.depth), 0, 0, (gx_device *)pmdev);
  1064.     mdev.width = param_dev->width;
  1065.     mdev.height = param_dev->height;
  1066.     /* BMP files need width rounded up so that a scan line is */
  1067.     /* a multiple of 4 bytes. */
  1068.     /* This is currently done by gdev_mem_raster(). */
  1069.     /* It may be better to do it here explicitly in case */
  1070.     /* gdev_mem_raster changes. */
  1071.     raster = gdev_mem_raster(&mdev);
  1072.     data_size = (ulong)raster * mdev.height;
  1073.     ptr_size = sizeof(byte **) * mdev.height;
  1074.     pal_size = pm_palette_size(pmdev);
  1075.     needed = pmdev->bmi->cbFix + pal_size + data_size + ptr_size;
  1076.     /* round up to page boundary */
  1077.     needed = (needed + MIN_COMMIT - 1) & (~(MIN_COMMIT-1));
  1078.     if (needed > pmdev->committed) {
  1079.         /* commit more memory */
  1080.         if (rc = DosSetMem(pmdev->bitmap + pmdev->committed, 
  1081.             needed - pmdev->committed, 
  1082.             PAG_COMMIT | PAG_DEFAULT)) {
  1083.             fprintf(stderr,"No memory in pm_alloc_bitmap, rc = %d\n",rc);
  1084.             return gs_error_limitcheck;
  1085.         }
  1086.         pmdev->committed = needed;
  1087.     }
  1088.     /* Shared memory can't be decommitted */
  1089. #ifdef __DLL__
  1090.     if (pmdev->dll && (needed < pmdev->committed)) {
  1091.         /* decommit memory */
  1092.         if (rc = DosSetMem(pmdev->bitmap + needed, 
  1093.             pmdev->committed - needed, 
  1094.             PAG_DECOMMIT)) {
  1095.             fprintf(stderr,"Failed to decommit memory in pm_alloc_bitmap, rc = %d\n",rc);
  1096.             return gs_error_limitcheck;
  1097.         }
  1098.         pmdev->committed = needed;
  1099.     }
  1100. #endif
  1101.  
  1102.     /* Nothing can go wrong now.... */
  1103.     base = pmdev->bitmap + pmdev->bmi->cbFix + pm_palette_size(pmdev);
  1104.     pmdev->mdev = mdev;
  1105.     pmdev->mdev.base = (byte *)base;
  1106.     pmmproc(open_device)((gx_device *)&pmdev->mdev);
  1107.     pmdev->bmi->cbImage = data_size;
  1108.     return 0;
  1109. }
  1110.  
  1111. private void
  1112. pm_makepalette(gx_device_pm *pmdev)
  1113. {
  1114.     int i, val;
  1115.     RGB2 *argb = (RGB2 *)( (PBYTE)pmdev->bmi + pmdev->bmi->cbFix );
  1116.     if (pmdev->BitsPerPixel > 8)
  1117.         return;    /* these don't use a palette */
  1118.  
  1119.     for (i=0; i<pmdev->nColors; i++) {
  1120.       switch (pmdev->nColors) {
  1121.         case 64:
  1122.           /* colors are rrggbb */
  1123.           argb[i].bRed   = ((i & 0x30)>>4)*85;
  1124.           argb[i].bGreen = ((i & 0xC)>>2)*85;
  1125.           argb[i].bBlue  = (i & 3)*85;
  1126.           argb[i].fcOptions = 0;
  1127.           /* zero unused entries */
  1128.           argb[i+64].bRed = argb[i+64].bGreen = argb[i+64].bBlue  = 0;
  1129.           argb[i+64].fcOptions = 0;
  1130.           argb[i+128].bRed = argb[i+128].bGreen = argb[i+128].bBlue  = 0;
  1131.           argb[i+128].fcOptions = 0;
  1132.           argb[i+192].bRed = argb[i+192].bGreen = argb[i+192].bBlue  = 0;
  1133.           argb[i+192].fcOptions = 0;
  1134.           break;
  1135.         case 16:
  1136.           /* colors are irgb */
  1137.           val = (i & 8 ? 255 : 128);
  1138.           argb[i].bRed   = i & 4 ? val : 0;
  1139.           argb[i].bGreen = i & 2 ? val : 0;
  1140.           argb[i].bBlue  = i & 1 ? val : 0;
  1141.           if (i == 8) {    /* light gray */
  1142.               argb[i].bRed   = 
  1143.               argb[i].bGreen = 
  1144.               argb[i].bBlue  = 192;
  1145.               argb[i].fcOptions = 0;
  1146.           }
  1147.           break;
  1148.         case 2:
  1149.           argb[i].bRed =
  1150.             argb[i].bGreen =
  1151.             argb[i].bBlue = (i ? 255 : 0);
  1152.           argb[i].fcOptions = 0;
  1153.           break;
  1154.       }
  1155.     }
  1156. }
  1157.  
  1158.  
  1159. /* Cause display to be updated periodically */
  1160. private void 
  1161. pm_update(gx_device_pm *pmdev)
  1162. {
  1163.     if (pmdev->updating)
  1164.         return;
  1165.     if (!pmdev->UpdateInterval)
  1166.         return;
  1167.     if (*pmdev->GSVIEW) {
  1168.         APIRET rc;
  1169.         rc = DosWriteQueue(pmdev->drv_queue, GS_UPDATING, 0, NULL, 0);
  1170.             if (rc)
  1171.         fprintf(stderr,"pm_update: DosWriteQueue error %d\n",rc);
  1172.     }
  1173.     else {
  1174.         DosStartTimer(pmdev->UpdateInterval, (HSEM)pmdev->sync_event, 
  1175.         &pmdev->update_timer);
  1176.     }
  1177.     pmdev->updating = TRUE;
  1178. }
  1179.  
  1180. private uint
  1181. pm_set_bits_per_pixel(gx_device_pm *pmdev, int bpp)
  1182. {
  1183. static const gx_device_color_info pm_24bit_color = dci_color(24,255,255);
  1184. static const gx_device_color_info pm_8bit_color = dci_color(8,31,4);
  1185. static const gx_device_color_info pm_4bit_color = dci_pc_4bit;
  1186. static const gx_device_color_info pm_2color = dci_black_and_white;
  1187.     switch (bpp) {
  1188.         case 24:
  1189.         pmdev->color_info = pm_24bit_color;
  1190.         pmdev->nColors = (1<<24);
  1191.         break;
  1192.         case 8:
  1193.         /* use 64 static colors and 166 dynamic colors from 8 planes */
  1194.         pmdev->color_info = pm_8bit_color;
  1195.         pmdev->nColors = 64;
  1196.         break;
  1197.         case 4:
  1198.         pmdev->color_info = pm_4bit_color;
  1199.         pmdev->nColors = 16;
  1200.         break;
  1201.         case 1:
  1202.         pmdev->color_info = pm_2color;
  1203.         pmdev->nColors = 2;
  1204.         break;
  1205.         default:
  1206.         return (gs_error_rangecheck);
  1207.     }
  1208.     pmdev->BitsPerPixel = bpp;
  1209.     return 0;
  1210. }
  1211.  
  1212. /* return length of BMP palette in bytes */
  1213. private uint
  1214. pm_palette_size(gx_device_pm *pmdev)
  1215. {
  1216.     switch(pmdev->color_info.depth) {
  1217.         case 24:
  1218.         return 0;
  1219.         case 8:
  1220.         return 256*sizeof(RGB2);
  1221.         case 4:
  1222.         return 16*sizeof(RGB2);
  1223.     }
  1224.     /* must be two color */
  1225.     return 2*sizeof(RGB2);
  1226. }
  1227.  
  1228. /* This is used for testing */
  1229. /* Write out a BMP file to "out.bmp" */
  1230. private void 
  1231. pm_write_bmp(gx_device_pm *pmdev)
  1232. {
  1233.     BITMAPFILEHEADER2 bmfh;
  1234.     uint bmfh_length = sizeof(BITMAPFILEHEADER2) - sizeof(BITMAPINFOHEADER2);
  1235.     uint length;    /* bitmap length */
  1236.     ULONG fh;    /* file handle */
  1237.     ULONG action;
  1238.     ULONG count;
  1239.     bmfh.usType = 0x4d42;    /* "BM" */
  1240.     length = pmdev->bmi->cbFix + pm_palette_size(pmdev)
  1241.        + ( (gdev_mem_raster(&pmdev->mdev) * pmdev->mdev.height) );
  1242.     bmfh.cbSize = bmfh_length + length;
  1243.     bmfh.xHotspot = bmfh.yHotspot = 0;
  1244.     bmfh.offBits = bmfh_length + pmdev->bmi->cbFix + pm_palette_size(pmdev);
  1245.     if (DosOpen("out.bmp",    /* filename */
  1246.         &fh,        /* pointer to handle */
  1247.         &action,    /* pointer to result */
  1248.         0,        /* initial length */
  1249.         FILE_NORMAL,    /* normal file */
  1250.         OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS,
  1251.         OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYREADWRITE,
  1252.         0)) {
  1253.         fprintf(stderr,"error opening out.bmp\n");
  1254.         return;
  1255.     }
  1256.     if (DosWrite(fh, (PBYTE)&bmfh, bmfh_length, &count))
  1257.         fprintf(stderr,"error writing header for out.bmp\n");
  1258.     if (DosWrite(fh, pmdev->bitmap, length, &count))
  1259.         fprintf(stderr,"error writing out.bmp\n");
  1260.     if (DosClose(fh))
  1261.         fprintf(stderr,"error closing out.bmp\n");
  1262. }
  1263.